/* Functionality for right nav feedback module */
$(document).ready(function(){
	Feedback.init();
});
Feedback = {
	init:function(){
		Feedback.$this = $('#module-left-feedback');
		Feedback.$toggle = Feedback.$this.find('#feedback-toggle');
		Feedback.$title = Feedback.$this.find('.module-left-title').css('cursor','pointer');
		Feedback.$contents = Feedback.$this.find('.feedback-contents');
		Feedback.$submit = Feedback.$this.find('button');
		Feedback.$title.on('click',function(){
			Feedback.toggle();
		});
		Feedback.$toggle.on('click',function(){
			Feedback.toggle();
		});
		Feedback.$contents.find('.feedback-tab').on('click',function(){
			if(!$(this).hasClass('selected')){
				Feedback.selectTab(this);
			}
		});
		// open discussion if discussionID exists in url
		var discussionID = Main.getUrlArg("discussionID");
		if(discussionID.length > 0){
			var open = $('[discussionID='+discussionID+']').find('.fa-plus-square-o').get(0);
			Feedback.expandDiscussion(open);
		}
	},
	toggle:function(){
		var $content = Feedback.$contents;
		var $toggle = Feedback.$toggle;
		if(!$content.hasClass('transition')){
			if($content.hasClass('closed')){
				Feedback.$submit.hide();
				$toggle.addClass('fa-spin');
				$content.addClass('transition');
				$content.removeClass('closed');
				$content.slideDown(function(){
					$content.removeClass('transition');
					$toggle.removeClass('fa-spin');
					$toggle.removeClass('fa-toggle-down');
					$toggle.addClass('fa-toggle-up');
					Feedback.$submit.fadeIn();
				});
			} else{
				$content.addClass('transition');
				Feedback.$submit.fadeOut(100);
				$toggle.addClass('fa-spin');
				$content.addClass('closed');
				$content.slideUp(function(){
					$toggle.removeClass('fa-spin');
					$content.removeClass('transition');
					$toggle.addClass('fa-toggle-down');
					$toggle.removeClass('fa-toggle-up');
				});
			}
		}
	},
	selectTab:function(that){
		var $selected = $(that);
		var tab = $selected.attr('id').split('-')[2];
		var $discussionCategoryID = $('input[name=discussionCategoryID]');
		$discussionCategoryID.val($selected.attr('discussionCategoryID'));
		$('.feedback-tab.selected').removeClass('selected');
		$selected.addClass('selected');
		$('.feedback-title').addClass('hidden');
		$('.feedback-title.'+tab).removeClass('hidden');
	},
	expandDiscussion:function(that){
		var $this = $(that);
		var $close = $this.next();
		var $discussion = $this.closest('.feedback-open-item');
		var $messages = $discussion.find('.feedback-open-messages');
		var $remove = $discussion.find('[name=unfollow-discussion-real]').hide();
		// hide any open discussions
		$('.fa-minus-square-o').not('hidden').each(function(){
			Feedback.collapseDiscussion(this);
		});
		// do nothing if animating
		if($messages.hasClass('animating')){
			return;
		}
		if($messages.html() == ""){
			// load messages
			$this.addClass('fa-spin');
			Feedback.loadMessages($this,$messages,$discussion.attr('discussionID'));
		} else {
			// expand messages
			$this.addClass('hidden');
			$close.removeClass('hidden');
			//$messages.removeClass('hidden');
			$messages.slideDown();
		}
		var $controls = $discussion.find('.feedback-open-messages-controls');
		$controls.removeClass('hidden');
	},
	loadMessages:function($this,$messages,discussionID){
		var $item = $this.closest('.feedback-open-item');
		var $created = $item.find('.feedback-open-item-created').find('span.new');
		$messages.addClass('animating');
		Ajax.run({
			method:"discussionView.renderMessages",
			onsuccess:function(data){
				$this.removeClass('fa-spin');
				$this.addClass('hidden');
				$this.next().removeClass('hidden');
				$messages.html(data);
				$messages.slideDown(function(){
					$messages.removeClass('animating');
				});
				Main.activateTextareaResize();
				if($created.length > 0){
					$created.remove();
					Feedback.calculateNewMessages();
				}
			},
			args:{discussionID:discussionID}
		});
	},
	calculateNewMessages:function(){
		var $feedback = $('#module-left-feedback');
		var $open = $feedback.find('.feedback-open');
		var $total = $feedback.find('.module-left-title').find('span.new');
		var count = 0;
		$open.find('span.new').each(function(){
			var text = $(this).text();
			text = text.replace('(',"");
			text = text.replace(')',"");
			var number = +text;
			if(!isNaN(number)){
				count += +number;
			}
		});
		if($total.length > 0){
			$total.text('('+count+')');
		}
	},
	collapseDiscussion:function(that){
		var $this = $(that);
		var $open = $this.prev();
		var $discussion = $this.closest('.feedback-open-item');
		var $messages = $discussion.find('.feedback-open-messages');
		if($messages.hasClass('animating')){
			return;
		}
		$messages.addClass('animating');
		$messages.slideUp(function(){
			$messages.removeClass('animating');
		});
		$this.addClass('hidden');
		$open.removeClass('hidden');
		var $controls = $discussion.find('.feedback-open-messages-controls');
		$controls.addClass('hidden');
	},
	showRespond:function(that){
		var $this = $(that);
		var $discussion = $this.closest('.feedback-open-item');
		var $placeholder = $discussion.find('.placeholderjs-wrapper[name=message]');
		var $submit = $discussion.find('button[name=submit-message]');
		var $textarea = $discussion.find('textarea[name=message]');
		var $unfollow = $discussion.find('button[name=unfollow-discussion]');
		var $cancel = $discussion.find('button[name=respond-message-cancel]');
		$textarea.removeClass('hidden');
		$textarea.focus();
		$this.addClass('hidden');
		$placeholder.css({opacity:1});
		$submit.removeClass('hidden');
		$discussion.find('.feedback-open-message.empty').addClass('hidden');
		$unfollow.addClass('hidden');
		$cancel.removeClass('hidden');
	},
	cancelRespond:function(that){
		var $this = $(that);
		var $discussion = $this.closest('.feedback-open-item');
		var $placeholder = $discussion.find('.placeholderjs-wrapper[name=message]');
		var $submit = $discussion.find('button[name=submit-message]');
		var $textarea = $discussion.find('textarea[name=message]');
		var $unfollow = $discussion.find('button[name=unfollow-discussion]');
		var $cancel = $discussion.find('button[name=respond-message-cancel]');
		var $respond = $discussion.find('button[name=respond-message]');
		$this.addClass('hidden');
		$textarea.addClass('hidden');
		$submit.addClass('hidden');
		$placeholder.css({opacity:0});
		$discussion.find('.feedback-open-message.empty').removeClass('hidden');
		$unfollow.removeClass('hidden');
		$cancel.addClass('hidden');
		$respond.removeClass('hidden');
	},
	showUnfollow:function(that){
		var $this = $(that);
		var $discussion = $this.closest('.feedback-open-item');
		var $unfollow = $discussion.find('button[name=unfollow-discussion-real]');
		$this.fadeOut(function(){
			$unfollow.fadeIn();
			setTimeout(function(){
				$unfollow.fadeOut(function(){
					$this.fadeIn();
				});
			},2500);
		});
	},
	followSuggestedDiscussion:function(discussionID){
		// clear subject and message from new discussion and put into new discussion
		var $form = $('form[name=support_feedback_form]');
		Feedback.clearSuggestions($form);
		var $message = $form.find('textarea[name=message]');
		var $subject = $form.find('input[name=subject]');
		var message = $message.val();
		var subject = $subject.val();
		Ajax.run({
			method:"discussionService.followDiscussion",
			onsuccess:function(data){
				Feedback.loadDiscussions(discussionID,subject,message);
			},
			args:{discussionID:discussionID}
		});
	},
	saveMessage:function(discussionID){
		var $discussion = $('.feedback-open-item[discussionID='+discussionID+']');
		var $form = $discussion.find('[name=add_message_form]');
		var message = $form.find('textarea[name=message]').val();
		if(Valid.check($form)){
			Ajax.run({
				method:"discussionService.saveMessage",
				onsuccess:function(data){
					Feedback.loadDiscussions(discussionID);
				},
				args:{discussionID:discussionID,message:message}
			});
		}
	},
	unfollowDiscussion:function(discussionID){
		$('.feedback-open-item[discussionID='+discussionID+']').fadeOut(function(){
			Ajax.run({
				method:"discussionService.unfollowDiscussion",
				onsuccess:function(data){
					Feedback.loadDiscussions();
				},
				args:{discussionID:discussionID}
			});
		});
	},
	getSuggestions:function(that){
		var $this = $(that);
		var $form = $this.closest('form');
		var subject = $form.find('input[name=name]').val() || "";
		var message = $form.find('textarea[name=message]').val() || "";
		var discussionCategoryID = $form.find('input[name=discussionCategoryID]').val();
		var $suggestions = $form.next();
		var $button = $form.find('button[name=submit-discussion]');
		var $wait = $form.find('button[name=submit-discussion-wait]');
		$button.hide();
		$wait.removeClass('hidden');
		var submit = false;
		if($suggestions.html().length > 0){
			submit = true;
		}
		Ajax.run({
			method:"discussionView.renderSuggestions",
			onsuccess:function(data){
				if(data.length > 1){
					$suggestions.html(data);
					$suggestions.fadeIn();
					// add message and subject to the suggestions form
					$suggestions.find('[type=hidden][name=subject]').val(subject);
					$suggestions.find('[type=hidden][name=message]').val(message);
					HoverBubble.init();
				} else {
					$suggestions.html('go');
					Feedback.saveDiscussion();
				}
			},
			args:{
				discussionCategoryID:discussionCategoryID,
				subject:subject
			}
		});
	},
	noMatch:function(that){
		var $this = $(that);
		var $feedback = $this.closest('#module-left-feedback');
		var $suggestions = $feedback.find('.feedback-suggestions');
		var $wait = $feedback.find('button[name=submit-discussion-wait]');
		var $button = $feedback.find('button[name=submit-discussion]');
		$suggestions.fadeOut(function(){
			$button.show().click();
		});
		$wait.addClass('hidden');
	},
	trySubmit:function(that){
		var $this = $(that);
		var $feedback = $this.closest('#module-left-feedback');
		var $suggestions = $feedback.find('.feedback-suggestions');
		var $form = $this.closest('form');
		if(!Valid.check($form)){
			return;
		}
		var result = $suggestions.html().length > 0;
		if(!result){
			Feedback.getSuggestions(that);
		} else {
			Feedback.saveDiscussion();
		}
		return;
	},
	saveDiscussion:function(){
		var $feedback = $('#module-left-feedback');
		var $form = $feedback.find('[name=support_feedback_form]');
		var categoryID = $form.find('[name=discussionCategoryID]').val();
		var name = $form.find('input[name=name]').val();
		var message = $form.find('textarea[name=message]').val();
		$form.find('input[name=name]').val('');
		$form.find('textarea[name=message]').val('');
		Ajax.run({
			method:"discussionService.saveInitialDiscussion",
			onsuccess:function(data){
				Feedback.loadDiscussions();
				var $button = $form.find('button[name=submit-discussion]');
				var $wait = $form.find('button[name=submit-discussion-wait]');
				$.button.show();
				$wait.addClass('hidden');
			},
			args:{
				discussionCategoryID:categoryID,
				name:name,
				message:message
			}
		});
	},
	loadDiscussions:function(discussionID,subject,message){
		var discussionID = discussionID || "";
		var subject = subject || "";
		var message = message || "";
		var $form = $('form[name=support_feedback_form]');
		var $subject = $form.find('input[name=name]');
		var $message = $form.find('textarea[name=message]');
		$subject.val('');
		$message.val('');
		Ajax.run({
			method:"discussionView.renderOpenDiscussions",
			onsuccess:function(data){
				$('.feedback-open').html(data);
				Placeholder.init();
				Validate.init();
				if(discussionID != ""){
					var $item = $('.feedback-open-item[discussionID='+discussionID+']');
					var $plus = $item.find('.fa.fa-plus-square-o');
					var $respond = $item.find('button[name=respond-message]');
					var $message = $item.find('textarea[name=message]');
					Feedback.expandDiscussion($plus);
					Feedback.showRespond($respond);
					$message.val(message);
				}
			},
			args:{discussionID:discussionID}
		});
	},
	clearSuggestions:function(that){
		var $this = $(that);
		var $feedback = $this.closest('#module-left-feedback');
		var $suggestions = $feedback.find('.feedback-suggestions');
		var $wait = $feedback.find('button[name=submit-discussion-wait]');
		var $button = $feedback.find('button[name=submit-discussion]');
		// fade out suggestions
		// fade in correct buttons
		$suggestions.html('');
		$suggestions.hide();
		$wait.addClass('hidden');
		$button.show();
	},
	showArchived:function(that){
		$this = $(that);
		$widget = $this.closest('.feedback-contents');
		$archived = $widget.find('.archived');
		$archived.removeClass('archived');
		$this.parent().hide();
	},
	toggleSuggestionMessages:function($icon){
		var discussionID = $icon.attr('discussionID');
		var userID = $icon.attr('userID');
		var $messages = $('.suggestion-messages[discussionID='+discussionID+']');
		if($messages.html() != ""){
			$messages.slideUp(function(){
				$(this).html('');
			});
			return;
		}
		$icon.removeClass('fa-comment-o').addClass('fa-spin fa-spinner');
		Ajax.run({
			method:"discussionService.getMessages",
			onsuccess:function(data){
				var nameIndex = 5;
				var businessIndex = 6;
				var messageIndex = 2;
				var userIDIndex = 4;
				$icon.removeClass('fa-spin fa-spinner').addClass('fa-comment-o');
				for(var i = 0; i < data.DATA.length; i++){
					var name = data.DATA[i][nameIndex];
					var business = data.DATA[i][businessIndex];
					if(business != ""){
						name = name + ' @ ' + business;
					}
					if(userID == data.DATA[i][userIDIndex]){
						name = "you";
					}
					name = name + " : ";
					var $person = $('<span class="suggestion-message-person">'+name+'</span>');
					if(userID == data.DATA[i][userIDIndex]){
						$person.css({color:"rgb(184, 98, 41)"});
					}
					
					var $text = $('<span class="suggestion-message-text">'+data.DATA[i][messageIndex]+'</span>');
					var $message = $('<div class="suggestion-message"></div>').append($person).append($text);
					$messages.append($message);
				}
				$messages.fadeIn();
			},
			args:{discussionID:discussionID}
		});
	}
};